import { createStore } from 'vuex'
import getters from './getters'

// 批量导入 ./modules 目录下的所有模块
const modulesFiles = require.context('./modules', true, /\.js$/)
const modules = modulesFiles.keys().reduce((modules, modulePath) => {
  const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1')
  const value = modulesFiles(modulePath)
  modules[moduleName] = value.default
  return modules
}, {})

const state = {
  channelId: '',
  phoneTokenMap: {},
  appConfig: {}
}

const mutations = {
  SET_CHANNEL_ID: (state, value) => {
    state.channelId = value
  },
  SET_MAP: (state, options) => {
    const { key, value, mapKey } = options
    state[key][mapKey] = value
  },
  SET_APP_CONFIG: (state, value) => {
    state.appConfig = value
  }
}

const actions = {
  setAppConfig: () => {
  }
}

const store = createStore({
  state,
  mutations,
  actions,
  getters,
  modules,
})

export default store
